﻿using ZYCommon;
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Windows32;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
    public class ZYTextTable : ZYTextTableBase
    {
        private Rectangle dragRect;
        private int lastX;
        private int lastY;
        private MouseMoveType mouseMoveType;
        private int oldX;
        private int oldY;
        private ArrayList pagesLinesPos;
        private ZYTextTableCell resizeCell;
        private ZYTextTableRow resizeRow;
        private int resizeType;
        private bool rightEdge;
        private bool isRefresh = false;
        private ArrayList selectedCells;

        public ZYTextTable()
            : base(0)
        {
            this.selectedCells = new ArrayList();
            this.lastX = 0;
            this.lastY = 0;
            this.dragRect = Rectangle.Empty;
            this.mouseMoveType = MouseMoveType.mmtCommon;
            this.resizeType = 0;
            this.oldX = 0;
            this.oldY = 0;
            this.resizeRow = null;
            this.resizeCell = null;
            this.rightEdge = false;
            this.pagesLinesPos = new ArrayList();
            this.Init();
        }

        public ZYTextTable(ZYTextContainer owner)
            : base(0)
        {
            this.selectedCells = new ArrayList();
            this.lastX = 0;
            this.lastY = 0;
            this.dragRect = Rectangle.Empty;
            this.mouseMoveType = MouseMoveType.mmtCommon;
            this.resizeType = 0;
            this.oldX = 0;
            this.oldY = 0;
            this.resizeRow = null;
            this.resizeCell = null;
            this.rightEdge = false;
            this.pagesLinesPos = new ArrayList();
            this.Init();
            this.Parent = owner;
        }

        public ZYTextTable(ZYTextDocument ownerDocument, ZYTextContainer owner, int rowCount, int colCount)
            : base(0)
        {
            this.selectedCells = new ArrayList();
            this.lastX = 0;
            this.lastY = 0;
            this.dragRect = Rectangle.Empty;
            this.mouseMoveType = MouseMoveType.mmtCommon;
            this.resizeType = 0;
            this.oldX = 0;
            this.oldY = 0;
            this.resizeRow = null;
            this.resizeCell = null;
            this.rightEdge = false;
            this.pagesLinesPos = new ArrayList();
            this.Init();
            base.myOwnerDocument = ownerDocument;
            base.myParent = owner;
            try
            {
                for (int i = 0; i < rowCount; i++)
                {
                    ZYTextTableRow createRow = this.CreateRow();
                    if (createRow != null)
                    {
                        for (int j = 0; j < colCount; j++)
                        {
                            ZYTextTableCell cell = this.CreateCell(createRow);
                            if (cell != null)
                            {
                                createRow.AppendCell(cell);
                            }
                            else
                            {
                                MessageBox.Show("创建单元格失败!");
                                return;
                            }
                        }
                        this.AppendRow(createRow);
                    }
                    else
                    {
                        MessageBox.Show("创建表格行失败!");
                        return;
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        public ZYTextTableRow _InsertRow(ZYTextTableRow refRow, int pos)
        {
            int index = this.Rows.IndexOf(refRow);
            return this._InsertRow(refRow, index, pos);
        }

        public void _InsertRow(int index, ZYTextTableRow row)
        {
            this.Rows.Insert(index, row);
        }

        public ZYTextTableRow _InsertRow(int index, int pos)
        {
            if ((index < 0) || (index > (this.Rows.Count - 1)))
            {
                return null;
            }
            ZYTextTableRow refRow = (ZYTextTableRow)this.Rows[index];
            return this._InsertRow(refRow, index, pos);
        }

        private ZYTextTableRow _InsertRow(ZYTextTableRow refRow, int index, int pos)
        {
            try
            {
                ZYTextTableRow createRow = this.CreateRow();
                for (int i = 0; i < refRow.CellCount; i++)
                {
                    ZYTextTableCell cell = this.CreateCell(createRow);
                    cell.Width = ((ZYTextTableCell)refRow.ChildElements[i]).Width;
                    cell.Height = ((ZYTextTableCell)refRow.ChildElements[i]).Height;
                    createRow.AppendCell(cell);
                }
                if ((pos == 0) && (index > 0))
                {
                    index--;
                }
                this._InsertRow(index, createRow);
                return createRow;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                return null;
            }
        }

        public override void AddElementToList(ArrayList myList, bool ResetFlag)
        {
            base.AddElementToList(myList, ResetFlag);
        }

        public void AppendRow(ZYTextTableRow row)
        {
            this.Rows.Add(row);
        }

        private void calcSelRegion(Rectangle dragRect)
        {
            this.selectedCells.Clear();
            ZYTextTableCell oneCell = null;
            foreach (ZYTextTableRow row in this.Rows)
            {
                foreach (ZYTextTableCell cell in row.ChildElements)
                {
                    if (this.selectedCells.Count > 0)
                    {
                        oneCell = this.selectedCells[0] as ZYTextTableCell;
                    }
                    if (cell.Bounds.IntersectsWith(dragRect))
                    {
                        if (this.selectedCells.Count > 1)
                        {
                            if (oneCell.Parent == ((ZYTextTableCell)this.selectedCells[1]).Parent)
                            {
                                if (cell.Parent == oneCell.Parent)
                                {
                                    cell.Selected = true;
                                    this.selectedCells.Add(cell);
                                }
                            }
                            else
                            {
                                if ((((ZYTextTableCell)this.selectedCells[1]).ColIndex == cell.ColIndex))
                                {
                                    cell.Selected = true;
                                    this.selectedCells.Add(cell);
                                }
                            }
                        }

                        if (selectedCells.Count < 2)
                        {
                            cell.Selected = true;
                            this.selectedCells.Add(cell);
                        }
                    }
                    else
                    {
                        cell.Selected = false;
                    }
                }
            }
            this.OwnerDocument.OwnerControl.Refresh();
        }

        /***
         * 合并单元格
         */
        private void CalVMergedCellHoldRowsCount()
        {
            if (this.RowCount > 1)
            {
                ZYTextTableCell cell = null;
                ZYTextTableCell cell2 = null;
                ZYTextTableRow refRow = null;
                try
                {
                    foreach (ZYTextTableRow row2 in this.Rows)
                    {
                        for (int i = 0; i < row2.CellCount; i++)
                        {
                            if (i >= row2.ChildElements.Count)
                                continue;
                            cell = (ZYTextTableCell)row2.ChildElements[i];
                            if (cell.VMStart)
                            {
                                refRow = this.GetNextRow(row2);
                                foreach (ZYTextTableCell childCell in refRow.ChildElements)
                                {
                                    if (cell.RealLeft == childCell.RealLeft && childCell.VMContinue)
                                    {
                                        cell2 = childCell;
                                        break;
                                    }
                                }
                                cell.HoldRowsCount = 1;
                                while (cell2 != null)
                                {
                                    cell.HoldRowsCount++;
                                    refRow = this.GetNextRow(refRow);
                                    if (refRow == null)
                                    {
                                        break;
                                    }
                                    cell2 = null;
                                    foreach (ZYTextTableCell childCell in refRow.ChildElements)
                                    {
                                        if (cell.RealLeft == childCell.RealLeft && childCell.VMContinue)
                                        {
                                            cell2 = childCell;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    ZYErrorReport.Instance.Clear();
                    ZYErrorReport.Instance.SourceException = exception;
                    ZYErrorReport.Instance.SourceObject = this;
                    ZYErrorReport.Instance.UserMessage = "计算表格内需纵向合并的单元格占据的行数错误！";
                    ZYErrorReport.Instance.MemberName = "CalVMergedCellHoldRowsCount";
                    ZYErrorReport.Instance.ShowErrorDialog();
                }
            }
        }

        private void CancelSelStatus()
        {
            foreach (ZYTextTableRow row in this.Rows)
            {
                foreach (ZYTextTableCell cell in row.ChildElements)
                {
                    cell.Selected = false;
                }
            }
        }

        public ZYTextTableCell CreateCell(ZYTextTableRow createRow)
        {
            try
            {
                ZYTextTableCell cell = new ZYTextTableCell();
                cell.OwnerTable = this;
                cell.OwnerDocument = base.myOwnerDocument;
                cell.Parent = createRow;
                cell.OwnerRow = createRow;
                cell.IsLoadFromFile = false;
                return cell;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                return null;
            }
        }

        public ZYTextTableRow CreateRow()
        {
            try
            {
                ZYTextTableRow row = new ZYTextTableRow();
                row.OwnerTable = this;
                row.Parent = this;
                row.OwnerDocument = base.myOwnerDocument;
                return row;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                return null;
            }
        }

        public void DrawResizeLine(int type, int x, int y)
        {
            Graphics graphics = null;
            if (this.OwnerDocument.OwnerControl != null)
            {
                graphics = this.OwnerDocument.OwnerControl.CreateGraphics();
            }
            int hDC = graphics.GetHdc().ToInt32();
            int drawMode = Gdi32.GetROP2(hDC);
            int hObject = Gdi32.CreatePen(1, 1, 0xff);
            int num4 = Gdi32.SelectObject(hDC, hObject);
            Gdi32.SetROP2(hDC, 7);
            if (type == 0)
            {
                Gdi32.MoveToEx(hDC, 0, y, 0);
                Gdi32.LineTo(hDC, 827, y);
            }
            else if (type == 1)
            {
                Gdi32.MoveToEx(hDC, x, 0, 0);
                Gdi32.LineTo(hDC, x, 1169);
            }
            Gdi32.SelectObject(hDC, num4);
            Gdi32.DeleteObject(hObject);
            Gdi32.SetROP2(hDC, drawMode);
            graphics.ReleaseHdc(new IntPtr(hDC));
        }

        public override bool FromXML(XmlElement myElement)
        {
            if (base.FromXML(myElement))
            {
                foreach (ZYTextElement element in base.myChildElements)
                {
                    if (element is ZYTextTableRow)
                    {
                        ZYTextTableRow row = element as ZYTextTableRow;
                        if (row.Parent == this)
                        {
                            row.OwnerTable = this;
                        }
                    }
                }
                return true;
            }
            return false;
        }

        public bool TableLock
        {
            get
            {
                return base.Attributes.GetBool("tableLock");
            }
            set
            {
                base.Attributes.SetValue("tableLock", value);
            }
        }

        public int GetNearestLeft(ZYTextTableCell curCell)
        {
            int right = -1;
            foreach (ZYTextTableRow row in curCell.OwnerTable.Rows)
            {
                foreach (ZYTextTableCell cell in row.ChildElements)
                {
                    if (cell.Bounds.Right <= curCell.Bounds.Left)
                    {
                        if (right == -1)
                        {
                            right = cell.Bounds.Right;
                        }
                        if (cell.Bounds.Right > right)
                        {
                            right = cell.Bounds.Left;
                        }
                    }
                }
            }
            if (right == -1)
            {
                right = curCell.OwnerRow.RealLeft;
            }
            return right;
        }

        public int GetNearestRight(ZYTextTableCell curCell)
        {
            int left = -1;
            foreach (ZYTextTableRow row in curCell.OwnerTable.Rows)
            {
                foreach (ZYTextTableCell cell in row.ChildElements)
                {
                    if (cell.Bounds.Left >= curCell.Bounds.Right)
                    {
                        if (left == -1)
                        {
                            left = cell.Bounds.Left;
                        }
                        if (cell.Bounds.Left < left)
                        {
                            left = cell.Bounds.Left;
                        }
                    }
                }
            }
            if (left == -1)
            {
                left = curCell.Bounds.Right;
            }
            return left;
        }

        public ZYTextTableCell GetNextCell(ZYTextTableCell curCell)
        {
            ZYTextTableRow ownerRow = curCell.OwnerRow;
            if (ownerRow.IsLastCell(curCell))
            {
                if (this.IsLastRow(ownerRow))
                {
                    return null;
                }
                ZYTextTableRow row2 = (ZYTextTableRow)this.Rows[this.Rows.IndexOf(ownerRow) + 1];
                return row2.FirstCell;
            }
            return (ZYTextTableCell)ownerRow.ChildElements[ownerRow.ChildElements.IndexOf(curCell) + 1];
        }

        public ZYTextTableRow GetNextRow(ZYTextTableRow refRow)
        {
            if (!this.Rows.Contains(refRow))
            {
                return null;
            }
            if (this.IsLastRow(refRow))
            {
                return null;
            }
            int index = this.Rows.IndexOf(refRow);
            return (ZYTextTableRow)this.Rows[index + 1];
        }

        public override string GetXMLName()
        {
            return "table";
        }

        public override bool HandleClick(int x, int y, MouseButtons Button)
        {
            return base.HandleClick(x, y, Button);
        }

        public override bool HandleDblClick(int x, int y, MouseButtons Button)
        {
            return base.HandleDblClick(x, y, Button);
        }

        public override bool HandleMouseDown(int x, int y, MouseButtons Button)
        {
            isRefresh = true;
            this.dragRect = Rectangle.Empty;
            this.CancelSelStatus();
            if (this.Bounds.Contains(x, y))
            {
                if (!this.TableLock)
                {
                    int num = x;
                    int num2 = y;
                    if (Button == MouseButtons.Left)
                    {
                        if (this.InHLine(num, num2, this.resizeRow))
                        {
                            this.oldX = num;
                            this.oldY = num2;
                            this.resizeType = 0;
                            this.mouseMoveType = MouseMoveType.mmtResize;
                        }
                        else if (this.InVLine(num, num2, ref this.resizeCell))
                        {
                            this.oldX = num;
                            this.oldY = num2;
                            this.resizeType = 1;
                            this.mouseMoveType = MouseMoveType.mmtResize;
                        }
                        else
                        {
                            this.lastX = num;
                            this.lastY = num2;
                            this.mouseMoveType = MouseMoveType.mmtSelect;
                        }
                    }
                    else
                    {
                        this.mouseMoveType = MouseMoveType.mmtCommon;
                    }
                    Happend(x, y);
                }
            }
            return base.HandleMouseDown(x, y, Button);
        }

        public override bool HandleMouseMove(int x, int y, MouseButtons Button)
        {
            if (this.Bounds.Contains(x, y))
            {
                int num = x;
                int num2 = y;
                if (!this.TableLock)
                {
                    if (Button != MouseButtons.Left)
                    {
                        if (this.InHLine(num, num2, this.resizeRow))
                        {
                            this.OwnerDocument.SetCursor(System.Windows.Forms.Cursors.HSplit);
                        }
                        else if (this.InVLine(num, num2, ref this.resizeCell))
                        {
                            this.OwnerDocument.SetCursor(System.Windows.Forms.Cursors.VSplit);
                        }
                        else
                        {
                            this.OwnerDocument.SetCursor(System.Windows.Forms.Cursors.IBeam);
                        }
                    }
                    else
                    {
                        switch (this.mouseMoveType)
                        {
                            case MouseMoveType.mmtSelect:
                                if (this.Bounds.Contains(this.lastX, this.lastY))
                                {
                                    if ((this.lastX >= x) || (this.lastY >= y))
                                    {
                                        if ((this.lastX > x) && (this.lastY < y))
                                        {
                                            this.dragRect.X = x;
                                            this.dragRect.Y = y;
                                            this.dragRect.Width = this.lastX - x;
                                            this.dragRect.Height = y - this.lastY;
                                        }
                                        else if ((this.lastX > x) && (this.lastY > y))
                                        {
                                            this.dragRect.X = x;
                                            this.dragRect.Y = this.lastY;
                                            this.dragRect.Width = this.lastX - x;
                                            this.dragRect.Height = this.lastY - y;
                                        }
                                        else if ((this.lastX < x) && (this.lastY > y))
                                        {
                                            this.dragRect.X = this.lastX;
                                            this.dragRect.Y = y;
                                            this.dragRect.Width = x - this.lastX;
                                            this.dragRect.Height = this.lastY - y;
                                        }
                                    }
                                    else
                                    {
                                        this.dragRect.X = this.lastX;
                                        this.dragRect.Y = this.lastY;
                                        this.dragRect.Width = x - this.lastX;
                                        this.dragRect.Height = y - this.lastY;
                                    }
                                    this.calcSelRegion(this.dragRect);
                                    if (isRefresh)
                                        base.myOwnerDocument.OwnerControl.Refresh();
                                }
                                return base.HandleMouseMove(x, y, Button);
                        }
                    }
                }
                return base.HandleMouseMove(x, y, Button);
            }
            return base.HandleMouseMove(x, y, Button);
        }

        public bool Happend(int x, int y)
        {
            if (base.myOwnerDocument.OwnerControl != null)
            {
                if (this.mouseMoveType == MouseMoveType.mmtResize)
                {
                    Point leftPoint = Point.Empty;
                    Point rightPoint = Point.Empty;
                    int moveResizeType = 0;
                    if (this.resizeCell == null)
                        return false;
                    if (this.resizeType == 1)
                    {
                        if (this.resizeCell.OwnerRow.IsFirstCell(this.resizeCell))
                        {
                            leftPoint.X = this.resizeCell.Bounds.Left + this.resizeCell.MinWidth;
                            if (!this.resizeCell.OwnerRow.IsLastCell(this.resizeCell))
                            {
                                rightPoint.X = this.resizeCell.OwnerRow.GetNextCell(this.resizeCell).Left - this.resizeCell.MinWidth;
                            }
                            moveResizeType = 1;
                            if (this.resizeCell.OwnerRow.CellCount == 1)
                            {
                                if (rightEdge)
                                {
                                    rightPoint.X = rightPoint.X = this.Parent.Width;
                                    moveResizeType = 2;
                                }
                                else
                                {
                                    rightPoint.X = this.resizeCell.Width - this.resizeCell.MinWidth;
                                    moveResizeType = 1;
                                }
                            }
                        }
                        else if (this.resizeCell.OwnerRow.IsLastCell(this.resizeCell))
                        {
                            if (rightEdge)
                            {
                                leftPoint.X = this.resizeCell.Left + this.resizeCell.MinWidth;
                                rightPoint.X = this.Parent.Width;
                                moveResizeType = 2;
                            }
                            else
                            {
                                leftPoint.X = this.resizeCell.OwnerRow.GetPreCell(this.resizeCell).Bounds.Left + this.resizeCell.MinWidth;
                                rightPoint.X = this.resizeCell.Bounds.Right - this.resizeCell.MinWidth;
                                moveResizeType = 3;
                            }
                        }
                        else
                        {
                            leftPoint.X = this.resizeCell.OwnerRow.GetPreCell(this.resizeCell).Bounds.Left + this.resizeCell.MinWidth;
                            rightPoint.X = this.resizeCell.Bounds.Right - this.resizeCell.MinWidth; 
                            moveResizeType = 3;
                        }
                        leftPoint.Y = y;
                        rightPoint.Y = y;
                    }
                    else
                    {

                    }
                    Point p = this.OwnerDocument.OwnerControl.CaptureDragPoint(this.resizeType, this.OwnerDocument.OwnerControl.ViewPointToClient(leftPoint.X, leftPoint.Y), this.OwnerDocument.OwnerControl.ViewPointToClient(rightPoint.X, rightPoint.Y));
                    Point p1 = this.OwnerDocument.OwnerControl.PointToView(p.X, p.Y);
                    if (p1.X == 0)
                        return false;
                    if (this.resizeType == 1)
                    {
                        foreach (ZYTextTableRow row in this.Rows)
                        {
                            foreach (ZYTextTableCell cell in row.Cells)
                            {
                                if (moveResizeType == 1)
                                {
                                    if (cell.Left == this.resizeCell.Left)
                                    {
                                        int chazhi = this.resizeCell.RealLeft - p1.X;
                                        cell.Width = cell.Width + chazhi;
                                        break;
                                    }
                                }
                                else if (moveResizeType == 2)
                                {
                                    if (cell.Bounds.Right == this.resizeCell.Bounds.Right)
                                    {
                                        int chazhi = this.resizeCell.Bounds.Right - p1.X;
                                        cell.Width = cell.Width - chazhi;
                                        break;
                                    }
                                }
                                else if (moveResizeType == 3)
                                {
                                    if (cell.Left == this.resizeCell.Left)
                                    {
                                        int chazhi = this.resizeCell.RealLeft - p1.X;
                                        cell.Width +=chazhi;
                                        cell.OwnerRow.GetPreCell(cell).Width -= chazhi;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    //this.RefreshSize();
                    base.OwnerDocument.RefreshLine();
                    this.OwnerDocument.ContentChanged();
                }
            }
            return true;
        }

        public override bool HandleMouseUp(int x, int y, MouseButtons Button)
        {

            #region
            //int num = x;
            //int num2 = y;
            //isRefresh = false;
            //if (!this.TableLock)
            //{
            //    if (this.mouseMoveType == MouseMoveType.mmtResize)
            //    {
            //        if (this.resizeType == 0)
            //        {
            //            this.DrawResizeLine(0, this.oldX, this.oldY);
            //            this.DrawResizeLine(0, num, num2);
            //        }
            //        else if (this.resizeType == 1)
            //        {
            //            int num3;
            //            int colIndex;
            //            ZYTextTableCell cell = null;
            //            if (this.resizeCell.OwnerRow.IsFirstCell(this.resizeCell) && this.resizeCell.OwnerRow.CellCount > 1)
            //            {
            //                foreach (ZYTextTableRow row in this.resizeCell.OwnerTable.Rows)
            //                {
            //                    colIndex = this.resizeCell.ColIndex;
            //                    if (colIndex >= row.CellCount)
            //                    {
            //                        colIndex = row.CellCount - 1;
            //                    }
            //                    if (colIndex >= row.ChildCount)
            //                        continue;
            //                    cell = (ZYTextTableCell)row.ChildElements[colIndex];
            //                    if (cell.Bounds.Left == this.resizeCell.Bounds.Left)
            //                    {
            //                        cell = (ZYTextTableCell)row.ChildElements[colIndex];
            //                        if (cell != null)
            //                        {
            //                            num3 = cell.Bounds.Left - this.oldX;
            //                            cell.Width += num3;
            //                            cell.Attributes.SetValue("width", cell.Width);
            //                        }
            //                    }
            //                }
            //            }
            //            else if (this.resizeCell.OwnerRow.IsLastCell(this.resizeCell) && this.rightEdge)
            //            {
            //                foreach (ZYTextTableRow row2 in this.resizeCell.OwnerTable.Rows)
            //                {
            //                    colIndex = this.resizeCell.OwnerRow.CellCount - 1;
            //                    if (colIndex >= row2.CellCount)
            //                    {
            //                        colIndex = row2.CellCount - 1;
            //                    }

            //                    if (colIndex >= row2.ChildCount)
            //                        continue;
            //                    cell = (ZYTextTableCell)row2.ChildElements[colIndex];
            //                    if (cell.Bounds.Left == this.resizeCell.Bounds.Left)
            //                    {
            //                        cell = (ZYTextTableCell)row2.ChildElements[colIndex];
            //                        if (cell != null)
            //                        {
            //                            num3 = cell.Bounds.Right - this.oldX;
            //                            cell.Width -= num3;
            //                            cell.Attributes.SetValue("width", cell.Width);
            //                        }
            //                    }
            //                }
            //            }
            //            else
            //            {
            //                ZYTextTableCell preCell = null;
            //                foreach (ZYTextTableRow row3 in this.resizeCell.OwnerTable.Rows)
            //                {
            //                    colIndex = this.resizeCell.ColIndex;
            //                    if (colIndex >= row3.CellCount)
            //                    {
            //                        colIndex = row3.CellCount - 1;
            //                    }
            //                    if (colIndex >= row3.ChildCount)
            //                        continue;
            //                    cell = (ZYTextTableCell)row3.ChildElements[colIndex];
            //                    if (cell.Bounds.Left == this.resizeCell.Bounds.Left)
            //                    {
            //                        preCell = row3.GetPreCell((ZYTextTableCell)row3.ChildElements[colIndex]);
            //                        if (preCell != null)
            //                        {
            //                            num3 = preCell.Bounds.Right - this.oldX;
            //                            preCell.Width -= num3;
            //                            preCell.Attributes.SetValue("width", preCell.Width);
            //                        }
            //                        colIndex = this.resizeCell.ColIndex;
            //                        if (colIndex >= row3.CellCount)
            //                        {
            //                            colIndex = row3.CellCount - 1;
            //                        }
            //                        cell = (ZYTextTableCell)row3.ChildElements[colIndex];
            //                        if (cell.Bounds.Left == this.resizeCell.Bounds.Left)
            //                        {
            //                            cell = (ZYTextTableCell)row3.ChildElements[colIndex];
            //                            if (cell != null)
            //                            {
            //                                num3 = cell.Bounds.Left - this.oldX;
            //                                cell.Width += num3;
            //                                cell.Attributes.SetValue("width", cell.Width);
            //                            }
            //                        }
            //                    }
            //                }
            //            }
            //        }
            //        this.OwnerDocument.ContentChanged();
            //    }
            //}
            //this.mouseMoveType = MouseMoveType.mmtCommon;
            //this.rightEdge = false;
            //this.OwnerDocument.SetCursor(System.Windows.Forms.Cursors.IBeam);
            #endregion

            return base.HandleMouseUp(x, y, Button);
        }

        private bool InHLine(int x, int y, ZYTextTableRow ownerRow)
        {
            if (this.Bounds.Contains(x, y))
            {
                foreach (ZYTextTableRow row in this.Rows)
                {
                    if (row.Bounds.Bottom == y)
                    {
                        ownerRow = row;
                        return true;
                    }
                }
                ownerRow = null;
            }
            return false;
        }

        private void Init()
        {
            base.myLastElement = null;
            base.myChildElements.Clear();
            base.myLines.Clear();
            base.ID = StringCommon.AllocObjectName();
            this.pagesLinesPos.Clear();
        }

        public bool InsertCol(ZYTextTableRow ownerRow, ZYTextTableCell refCell, int pos)
        {
            int index = ownerRow.ChildElements.IndexOf(refCell);
            if (index < 0)
            {
                return false;
            }
            ZYTextTableRow row = null;
            ZYTextTableCell cell = null;
            ZYTextTableCell cell2 = null;
            for (int i = 0; i < this.RowCount; i++)
            {
                row = (ZYTextTableRow)this.Rows[i];
                int num2 = index;
                if (row.CellCount <= index)
                {
                    num2 = row.CellCount - 1;
                }
                cell2 = (ZYTextTableCell)row.ChildElements[num2];
                cell = this.CreateCell(row);
                cell.Height = cell2.Height;
                int num3 = num2;
                if (pos == 0)
                {
                    num3 = num2;
                }
                else if (pos == 1)
                {
                    num3 = num2 + 1;
                }
                if (num3 >= row.CellCount)
                {
                    row.AppendCell(cell);
                }
                else
                {
                    row.InsertCell(num3, cell);
                }
                row.AverageCellWidth();
            }
            return true;
        }

        public ZYTextTableRow InsertRow(ZYTextTableRow refRow, int pos)
        {
            int index = this.Rows.IndexOf(refRow);
            return this.InsertRow(refRow, index, pos);
        }

        public void InsertRow(int index, ZYTextTableRow row)
        {
            this.Rows.Insert(index, row);
            base.myChildElements.Insert(index, row);
        }

        public ZYTextTableRow InsertRow(int index, int pos)
        {
            if ((index < 0) || (index > (this.Rows.Count - 1)))
            {
                return null;
            }
            ZYTextTableRow refRow = (ZYTextTableRow)this.Rows[index];
            return this.InsertRow(refRow, index, pos);
        }

        private ZYTextTableRow InsertRow(ZYTextTableRow refRow, int index, int pos)
        {
            try
            {
                ZYTextTableRow createRow = this.CreateRow();
                for (int i = 0; i < refRow.CellCount; i++)
                {
                    ZYTextTableCell cell = this.CreateCell(createRow);
                    cell.Width = ((ZYTextTableCell)refRow.ChildElements[i]).Width;
                    cell.Height = ((ZYTextTableCell)refRow.ChildElements[i]).Height;
                    createRow.AppendCell(cell);
                }
                if ((pos == 0) && (index > 0))
                {
                    index--;
                }
                this.InsertRow(index, createRow);
                return createRow;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                return null;
            }
        }

        private bool InVLine(int x, int y, ref ZYTextTableCell ownerCell)
        {
            int num = x;
            int num2 = y;
            if (this.Bounds.Contains(num, num2))
            {
                foreach (ZYTextTableRow row in this.Rows)
                {
                    foreach (ZYTextTableCell cell in row.ChildElements)
                    {
                        if (cell.Bounds.Contains(num, num2) && (cell.Bounds.Left == num))
                        {
                            ownerCell = cell;
                            this.rightEdge = false;
                            return true;
                        }
                        if (row.IsLastCell(cell) && (cell.Bounds.Right == num))
                        {
                            ownerCell = cell;
                            this.rightEdge = true;
                            return true;
                        }
                    }
                }
                this.rightEdge = false;
                ownerCell = null;
            }
            return false;
        }

        public bool IsLastRow(ZYTextTableRow row)
        {
            return (this.Rows.IndexOf(row) == (this.Rows.Count - 1));
        }

        public bool IsNeedRegulate()
        {
            bool flag = false;
            int num = 0;
            foreach (ZYTextTableRow row in this.Rows)
            {
                num = 0;
                foreach (ZYTextTableCell cell in row.ChildElements)
                {
                    num += cell.Width;
                }
                if (num > (row.Width - 11))
                {
                    row.Width = num + 11;
                    flag = true;
                }
            }
            return flag;
        }

        public override bool isNewLine()
        {
            return false;
        }

        public void MergeTableCCell()
        {
        }

        public void MergeTableHCell()
        {
        }

        public bool MergeTableHCell(ZYTextTableRow ownerRow, ArrayList cells)
        {
            if ((ownerRow == null) || (cells.Count < 2))
            {
                return false;
            }
            ZYTextTableCell cell = (ZYTextTableCell)cells[0];
            ZYTextTableCell cell2 = null;
            for (int i = 1; i < cells.Count; i++)
            {
                cell2 = (ZYTextTableCell)cells[i];
                cell.InsertRangeBefore(cell2.ChildElements.GetRange(0, cell2.ChildCount - 1), (ZYTextElement)cell.ChildElements[cell.ChildCount - 1]);
                for (int j = 0; j < cell2.ChildCount; j++)
                {
                    ((ZYTextElement)cell2.ChildElements[j]).Parent = cell;
                }
                cell.Width += cell2.Width;
                cell.Attributes.SetValue("width", cell.Width);
                ownerRow.RemoveChild(cell2);
            }
            return true;
        }

        public void MergeTableVCell()
        {
            if (this.selectedCells.Count >= 2)
            {
                ZYTextTableCell cell = (ZYTextTableCell)this.SelectedCells[0];
                cell.VMStart = true;
                ZYTextTableCell cell2 = null;
                for (int i = 1; i < this.selectedCells.Count; i++)
                {
                    cell2 = (ZYTextTableCell)this.selectedCells[i];
                    cell.Height += cell2.Height;
                    if (cell2.ChildCount > 1)
                    {
                        cell2.ChildElements.RemoveAt(cell2.ChildCount - 1);
                        cell.ChildElements.InsertRange(cell.ChildElements.Count - 1, cell2.ChildElements);
                    }
                    cell2.ChildElements.Clear();
                    cell2.VMContinue = true;
                    cell2.Visible = false;
                }
                for (int j = 0; j < cell.ChildCount; j++)
                {
                    ((ZYTextElement)cell.ChildElements[j]).Parent = cell;
                }
            }
        }

        public override bool OwnerWholeLine()
        {
            return false;
        }

        void RefreshWidth()
        {
            if (this.Width > this.Parent.Width)
            {
                this.Width = this.Parent.Width;
                base.Attributes.SetValue("width", base.intWidth);
            }
        }

        public override bool RefreshSize()
        {
            bool flag = false;
            if (base.Attributes.Contains("width"))
            {
                base.intWidth = base.Attributes.GetInt32("width");
                RefreshWidth();
            }
            else
            {
                if (base.intWidth == 0)
                {
                    flag = true;
                }
                base.intWidth = this.Parent.Width;
                base.Attributes.SetValue("width", base.intWidth);
            }
            base.intHeight = 0;
            foreach (ZYTextTableRow row in this.Rows)
            {
                row.RefreshSize();
                base.intHeight += row.Height;
            }
            RefreshWidth();
            if (flag)
            {
                this.RegulateTableWidth();
            }
            if (base.intWidth != this.Parent.Width)
            {
                base.intWidth = this.Parent.Width;
                base.Attributes.SetValue("width", base.intWidth);
            }
            if (this.IsNeedRegulate())
            {
                this.RegulateTableByLastCol();
            }
            this.CalVMergedCellHoldRowsCount();
            this.RegulateTableHeight();
            base.intHeight = 0;
            foreach (ZYTextTableRow row2 in this.Rows)
            {
                base.intHeight += row2.Height;
            }
            this.pagesLinesPos.Clear();
            return true;
        }

        public override bool RefreshView()
        {
            //this.CalVMergedCellHoldRowsCount();
            //this.RegulateTableHeight();
            foreach (ZYTextElement element in base.myChildElements)
            {
                element.RefreshView();
            }
            return true;
            //return base.RefreshView();
        }

        public void RegulateTableByLastCol()
        {
            //foreach (ZYTextTableRow row in this.Rows)
            //{
            //    int offset = base.intWidth - row.Width;
            //    row.Width = base.intWidth;
            //    row.RegulateColWidth(row.CellCount - 1, offset);
            //}
        }

        public void RegulateTableHeight()
        {
            if (this.RowCount > 1)
            {
                foreach (ZYTextTableRow row in this.Rows)
                {
                    row.RegulateVMRowHeight();
                }
                foreach (ZYTextTableRow row in this.Rows)
                {
                    row.RegulateVMRowHeight2();
                }
            }
        }

        public void RegulateTableWidth()
        {
            foreach (ZYTextTableRow row in this.Rows)
            {
                row.Width = base.intWidth;
                row.RegulateRowWidth();
            }
        }

        public void RemoveCell(ZYTextTableCell cell)
        {
        }

        public void RemoveCell(int rowIndex, int colIndex)
        {
        }

        public bool RemoveCol(ZYTextTableCell refCell)
        {
            int index = refCell.OwnerRow.ChildElements.IndexOf(refCell);
            if (index < 0)
            {
                return false;
            }
            for (int i = 0; i < this.RowCount; i++)
            {
                ZYTextTableRow row = (ZYTextTableRow)this.Rows[i];
                int num2 = index;
                if (row.CellCount <= index)
                {
                    num2 = row.CellCount - 1;
                }
                row.RemoveCell(num2);
            }
            return true;
        }

        public void RemoveRow(ZYTextTableRow row)
        {
            this.Rows.Remove(row);
            base.myChildElements.Remove(row);
        }

        public void RemoveRow(int index)
        {
        }

        public override bool ToXML(XmlElement myElement)
        {
            return base.ToXML(myElement);
        }
     
        public void RefreshRowHeight()
        {
            this.RegulateTableHeight();
            base.intHeight = 0;
            int width = 0;
            foreach (ZYTextTableRow row2 in this.Rows)
            {
                base.intHeight += row2.Height;
                if (width < row2.Width)
                {
                    width = row2.Width;
                }
                if (row2.OwnerLine.Height != row2.Height)
                    row2.OwnerLine.Height = row2.Height;
            }
            this.Width = width+2;
        }

        public int ColCount
        {
            get
            {
                int cellCount = 0;
                foreach (ZYTextTableRow row in this.Rows)
                {
                    if (cellCount < row.CellCount)
                    {
                        cellCount = row.CellCount;
                    }
                }
                return cellCount;
            }
        }

        public override int Height
        {
            get
            {
                return base.intHeight;
            }
            set
            {
                base.intHeight = value;
            }
        }

        public ArrayList PagesLinesPos
        {
            get
            {
                return this.pagesLinesPos;
            }
        }

        public int RowCount
        {
            get
            {
                return this.Rows.Count;
            }
        }

        public ArrayList Rows
        {
            get
            {
                return this.ChildElements;
            }
        }

        public ArrayList SelectedCells
        {
            get
            {
                return this.selectedCells;
            }
        }

        private enum MouseMoveType
        {
            mmtCommon,
            mmtSelect,
            mmtResize
        }
    }
}

